﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Wpf.CommonTools.StaticExtension
{
    /// <summary>
    /// 
    /// </summary>
    public static class FormExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="txtBox"></param>
        /// <param name="itemSource"></param>
        public static void ShowDropList(this TextBox txtBox, IEnumerable itemSource)
        {
            Popup p = new Popup();
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Gray);
            border.BorderThickness = new Thickness(0);

            StackPanel panel1 = new StackPanel();
            panel1.Background = new SolidColorBrush(Colors.White);

            ListBox list = new ListBox();
            list.Height = 100;
            list.Width = txtBox.Width;
            list.ItemsSource = itemSource;
            list.SelectionChanged += new SelectionChangedEventHandler((sender, e) =>   //添加选择到textbox空间上
            {
                txtBox.Text = list.SelectedItem.ToString();
                panel1.Width = txtBox.Width;
                if (txtBox.Text != "" || txtBox.Text != null)
                {
                    p.IsOpen = false;
                    txtBox.Focus();
                }
            });
            panel1.Children.Add(list);
            border.Child = panel1;
            p.Child = border;
            p.VerticalOffset = txtBox.Margin.Top;   //让创建的面板始终都在textbox空间的下方
            p.HorizontalOffset = txtBox.Margin.Left;
            Panel a = null;
            FrameworkElement parent = txtBox.Parent as FrameworkElement;
            do
            {
                a = parent as Panel;
                if (a != null)
                {
                    break;
                }
                else
                {
                    parent = parent.Parent as FrameworkElement;
                }

            } while (parent != null);
            if (a != null)
            {
                //a.Children.Add(p);
                //p.IsOpen = true;

                //Window window = Window.GetWindow(txtBox);
                //Point point = txtBox.TransformToAncestor(window).Transform(new Point(0, 0));
                //var addddd = p.TranslatePoint(new Point(), txtBox);
                //p.VerticalOffset = point.Y;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="alert"></param>
        /// <param name="parentForm"></param>
        public static void SetCenterOfWindow(this Window alert, Window parentForm)
        {
            alert.SizeChanged += (object sender, SizeChangedEventArgs e) =>
            {
                if (parentForm != null && parentForm.IsInitialized)
                {
                    Window child = (Window)sender;
                    child.WindowStartupLocation = WindowStartupLocation.Manual;
                    ParentFormStatus parent = (ParentFormStatus)parentForm.Dispatcher.Invoke(new Func<ParentFormStatus>(() =>
                    {
                        ParentFormStatus status = new ParentFormStatus()
                        {
                            State = parentForm.WindowState,
                            Height = parentForm.ActualHeight,
                            Width = parentForm.ActualWidth,
                            Left = parentForm.WindowState == WindowState.Maximized ? 0 : parentForm.Left,
                            Top = parentForm.WindowState == WindowState.Maximized ? 0 : parentForm.Top
                        };
                        return status;
                    }));

                    
                    child.Left = parent.Left + (parent.Width - child.ActualWidth) / 2;
                    child.Top = parent.Top + (parent.Height - child.ActualHeight) / 2;
                }
            };
        }

 

        class ParentFormStatus
        {
            public WindowState State;
            public double Width;
            public double Height;
            public double Left;
            public double Top;
        }
    }
}
